<?php

namespace app\model;

use app\utils\Md5Service;
use \think\facade\Db;
use think\facade\Log;

class PartnerModel
{

    /**
     * 根据手机号密码获取登录信息是否正确
     * @return void
     */
    public static function GetPartnerInfo($param)
    {
        return Db::table('xl_partner_user')->where('mobile', $param['mobile'])
            ->find();
    }

    /**
     * 根据手机号密码获取登录信息是否正确
     * @return void
     */
    public static function GetPartneInfo($param)
    {
        return Db::table('xl_partner_user')->where('mobile', $param['mobile'])
            ->where('password', Md5Service::encryptMd5($param['password'], $param['salt']))
            ->find();
    }

    public static function UpdatePartnerLoginInfo($data, $param, $newApiSalt)
    {
        $update['login_ip'] = $param['login_ip'];
        $update['api_salt'] = $newApiSalt;
        $update['login_num'] = $data['login_num'] + 1;
        $update['login_time'] = getNowDateTime();
        Db::startTrans();
        try {
            Db::table('xl_partner_user')->where(['id' => $data['id']])->update($update);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    public static function platforminfo($partner_id)
    {
        return Db::table('xl_partner')->where('id', '=', $partner_id)->find();
    }
}
